package com.winit.openapi.wh.validator;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.util.ValidateUtil;
import com.winit.openapi.wh.utils.CheckRepeatUtil;
import com.winit.openapi.wh.utils.PrecisionValidateUtils;
import com.winit.openapi.wh.utils.ValidateUtils;

/**
 * 海外仓入库单 校验器类 包裹信息
 * 
 * @version <pre>
 * Author	  Version		Date		Changes
 * dinghua.an	1.0  		2015年11月10日 	Created
 *
 * </pre>
 * @since 1.
 */
public class InboundPackagefieldValidator {

    /**
     * API下单，包裹信息校验
     * 
     * @param json
     */
    public static void validCreatePackageField(JSONObject json) {
        validatePackageInfo(json);
    }

    /**
     * 箱单信息上传功能 校验箱单信息
     * 
     * @param json
     */
    public static void validPackageField(JSONObject json) {
        // 入库单号
        ValidateUtil.validNotNull(json, "orderNo");
        ValidateUtil.validMaxLength(json, "orderNo", 64);
        //是否是金融入库单
        ValidateUtil.validMaxLength(json, "isFinance", 2, true);
        //金融入库单 贷款单ID必填
        if ("Y".equals(json.getString("isFinance"))) {
                ValidateUtil.validNotNull(json, "loanId");
                ValidateUtil.validMaxLength(json, "loanId", 20, true);
            }
        validatePackageInfo(json);
    }

    /**
     * 新增包裹信息功能 校验箱单信息
     * 
     * @param json
     */
    public static void ValidBoxOrderField(JSONObject json) {
        // 入库单号
        ValidateUtil.validNotNull(json, "orderNo");
        ValidateUtil.validMaxLength(json, "orderNo", 64);
        validateBaseInfo(json);
    }

    /**
     * 编辑箱单信息模块 校验箱单信息
     * 
     * @param json
     */
    public static void ValidEditOrderField(JSONObject json) {
        // 入库单号
        ValidateUtil.validNotNull(json, "orderNo");
        ValidateUtil.validMaxLength(json, "orderNo", 64);
        // 包裹Id
        ValidateUtil.validNotNull(json, "packageId");
        ValidateUtil.validMaxLength(json, "packageId", 11);
        validateBaseInfo(json);
    }

    /**
     * 构建包裹信息
     * 
     * @param json
     */
    private static void validatePackageInfo(JSONObject json) {
        // 包裹明细
        ValidateUtil.validNotNull(json, "packageList", "packageList");
        ValidateUtil.validList(json, "packageList", true);
        JSONArray packageList = json.getJSONArray("packageList");
        if (packageList.size() == 0) {
            throw new ApiException(ErrorCode.PACKAGE_CAN_NOT_NULL);
        }
        List<String> caseNoList = new ArrayList<String>();
        for (Object obj : packageList) {
            JSONObject packageDetails = (JSONObject) obj;
            ValidateUtil.validNotNull(packageDetails, "caseNo", "CaseNo");
            caseNoList.add(packageDetails.getString("caseNo"));
            validateBaseInfo(packageDetails);
        }
        // 校验箱号是否重复
        CheckRepeatUtil.hasSame(caseNoList, "caseNo");
    }

    /**
     * 基础信息校验
     * 
     * @param json
     */
    private static void validateBaseInfo(JSONObject json) {
        // 箱子编号
        ValidateUtil.validMaxLength(json, "caseNo", "CaseNo", 15, true);
        String caseNo =  json.getString("caseNo");
        // 重量
        //ValidateUtil.validMaxLength(json, "sellerWeight", 16);
        if (StringUtils.isNotEmpty(json.getString("sellerWeight"))) {
            PrecisionValidateUtils.validateIsNumber(json.getString("sellerWeight"), "sellerWeight");
            //PrecisionValidateUtils.precisionValidate(json.getBigDecimal("sellerWeight"), "sellerWeight", 6);
            PrecisionValidateUtils.precisionValidateMaxDigit(json.getBigDecimal("sellerWeight").setScale(6,
                BigDecimal.ROUND_HALF_UP),
                "sellerWeight",
                16);
        }

        // 长
        //ValidateUtil.validMaxLength(json, "sellerLength", 16);
        if (StringUtils.isNotEmpty(json.getString("sellerLength"))) {
            PrecisionValidateUtils.validateIsNumber(json.getString("sellerLength"), "sellerLength");
            //PrecisionValidateUtils.precisionValidate(json.getBigDecimal("sellerLength"), "sellerLength", 2);
            PrecisionValidateUtils.precisionValidateMaxDigit(json.getBigDecimal("sellerLength").setScale(2,
                BigDecimal.ROUND_HALF_UP),
                "sellerLength",
                16);
        }

        // 宽
        //ValidateUtil.validMaxLength(json, "sellerWidth", 16);
        if (StringUtils.isNotEmpty(json.getString("sellerWidth"))) {
            PrecisionValidateUtils.validateIsNumber(json.getString("sellerWidth"), "sellerWidth");
            //PrecisionValidateUtils.precisionValidate(json.getBigDecimal("sellerWidth"), "sellerWidth", 2);
            PrecisionValidateUtils.precisionValidateMaxDigit(json.getBigDecimal("sellerWidth").setScale(2,
                BigDecimal.ROUND_HALF_UP),
                "sellerWidth",
                16);
        }

        // 高
        //ValidateUtil.validMaxLength(json, "sellerHeight", 16);
        if (StringUtils.isNotEmpty(json.getString("sellerHeight"))) {
            PrecisionValidateUtils.validateIsNumber(json.getString("sellerHeight"), "sellerHeight");
            //PrecisionValidateUtils.precisionValidate(json.getBigDecimal("sellerHeight"), "sellerHeight", 2);
            PrecisionValidateUtils.precisionValidateMaxDigit(json.getBigDecimal("sellerHeight").setScale(2,
                BigDecimal.ROUND_HALF_UP),
                "sellerHeight",
                16);
        }

        // 箱单明细
        //ValidateUtil.validNotNull(json, "merchandiseList", "merchandiseList");
        if(json.containsKey("merchandiseList")){
            Object obj = json.get("merchandiseList");
            if(obj == null || StringUtils.isBlank(obj.toString())){
            	throw new ApiException(ErrorCode.NO_GOODS_IN_THE_PACKAGE,new Object[]{caseNo});
            }
        }
        //ValidateUtil.validList(json, "merchandiseList", true);
        JSONArray merchandiseList = json.getJSONArray("merchandiseList");
        if (merchandiseList == null || merchandiseList.size() == 0) {
            throw new ApiException(ErrorCode.NO_GOODS_IN_THE_PACKAGE,new Object[]{caseNo});
        }
        for (Object obj : merchandiseList) {
            JSONObject merchandiseDetails = (JSONObject) obj;
            // 产品编码
            ValidateUtil.validNotNull(merchandiseDetails, "merchandiseCode", "SKU No.");
            ValidateUtil.validMaxLength(merchandiseDetails, "merchandiseCode", "SKU No.", 255, true);
            // 规格
            ValidateUtil.validMaxLength(merchandiseDetails, "specifications", 255);
            // 数量
            ValidateUtil.validNotNull(merchandiseDetails, "quantity", "Quantity");
            ValidateUtil.validMaxLength(merchandiseDetails, "quantity", "Quantity", 8, true);
            // 校验必须是数字
            ValidateUtils.isNumber(merchandiseDetails.getString("quantity"), "quantity");
            // 校验预计数量大于0
            ValidateUtil.validNotZreo(merchandiseDetails, "quantity", "Quantity");
            //可下单数量
            if(merchandiseDetails.containsKey("canOrderQuantity"))
            {
                ValidateUtil.validNotNull(merchandiseDetails, "canOrderQuantity", "canOrderQuantity");
                ValidateUtil.validMaxLength(merchandiseDetails, "canOrderQuantity ", "canOrderQuantity ", 8, true);
                //校验本单数量不可大于可小单数量
                int quantity=merchandiseDetails.getInteger("quantity");
                int canOrderQuantity=merchandiseDetails.getInteger("canOrderQuantity");
                if(quantity>canOrderQuantity)
                {
                    throw new ApiException(ErrorCode.WH_ORDER_QUANTITY_BYOND_LIMIT_ERROR);
                }
            }
        }
    }
}
